package com.pro.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.pro.domain.*;
import com.pro.service.*;
import com.pro.service.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class BookController {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private BookService bookService;
    @Autowired
    private EvaluationService evaluationService;
    @Autowired
    private MemberService memberService;
    @Autowired
    private ShoucangService shoucangService;

    /*
    * 显示首页
    */
    @GetMapping("/")
    public ModelAndView showIndex(){
        ModelAndView modelAndView = new ModelAndView("/index");
        List<Category> categoryList = categoryService.selectAll();
        modelAndView.addObject("categoryList",categoryList);
        return modelAndView;
    }

    /**
     * 分页接口
     * @param page
     * @return
     */
    @GetMapping("/books")
    @ResponseBody
    public IPage<Book> selectBooks(Long categoryId,String order,Integer page){
        if(page==null){
            page=1;
        }
        IPage<Book> pageObject = bookService.paging(categoryId,order,page, 300);
        return pageObject;
    }

    /*
     * 活力下午茶界面
     */
    @GetMapping("/milk")
    public ModelAndView showMilkList(){
        ModelAndView modelAndView = new ModelAndView("/milk");
        List<Book> bookList = categoryService.selectOne();
        modelAndView.addObject("bookList",bookList);
        return modelAndView;
    }

    /**
     * 活力下午茶分页接口
     * @param page
     * @return
     */
    @GetMapping("/milkfunc")
    @ResponseBody
    public IPage<Book> selectBooks1(Long categoryId,String order,Integer page){
        if(page==null){
            page=1;
        }
        IPage<Book> pageObject = bookService.paging(categoryId,order,page, 30);
        return pageObject;
    }

    /*--------------------------------*/
    /*
     * 我的收藏界面
     */
    @GetMapping("/mysave")
    public ModelAndView showMysaveList(HttpSession session){
        ModelAndView modelAndView = new ModelAndView("/mysave");
        List<Book> bookList = categoryService.selectMysave(session);
        modelAndView.addObject("bookList",bookList);
        return modelAndView;
    }

    /**
     * 我的收藏分页接口
     * @param page
     * @return
     */
    @GetMapping("/mysavefunc")
    @ResponseBody
    public IPage<Book> selectBooks2(HttpSession session,Long categoryId,String order,Integer page){
        if(page==null){
            page=1;
        }
        IPage<Book> pageObject = bookService.paging2(session,categoryId,order,page, 30);
        return pageObject;
    }
    /*--------------------------------*/


    /**
     * 图书详情
     * @param bookId
     * @return
     */
    @GetMapping("/book/{bookId}")
    public ModelAndView displayBookDetail(@PathVariable("bookId")Long bookId, HttpSession session){

        ModelAndView mav=new ModelAndView("/detail");
        //1
        Book book = bookService.getBookById(bookId);
        //2
        List<Evaluation> evaluationList = evaluationService.getEvaluationByBookId(bookId);
        //3 从session中取得登录信息
        Member member = (Member) session.getAttribute("loginMember");
        if(member!=null){
            MemberReadState memberReadState = memberService.selectMemberReadState(member.getMemberId(), bookId);
            mav.addObject("memberReadState",memberReadState);
        }

        mav.addObject("book",book);
        mav.addObject("evaluationList",evaluationList);

        //注意，此时的mav中有了三样东西，book，评论列表，还有状态
        return mav;
    }

    /**
     * 增加收藏状态以及取消收藏状态
     * @param bookId
     * @param memberId
     * @return
     */
    @PostMapping("/shoucang")
    @ResponseBody
    public Integer shoucang(Long bookId, Long memberId){
        Integer result=shoucangService.selectOne(bookId,memberId);
        return result;
    }

    /**
     * 增加收藏状态以及取消收藏状态  只查，不修改！
     * @param bookId
     * @param memberId
     * @return
     */
    @PostMapping("/chashoucang")
    @ResponseBody
    public Integer chashoucang(Long bookId, Long memberId){
        Integer result=shoucangService.selectTwo(bookId,memberId);
        return result;
    }

}
